var WxParse = require('../../wxParse/wxParse.js');
Page({
    data: {
        arr:[1,2,3],
        id:'',
        header:{},
        userInfo:{},
        obj:{},
        atttionShow: false, // 默认未关注
        commentItem:{}, // 评论列表
        textareaVal: '',//发表评论输入框的值
        replynum:-1,// 是否显示回复的输入框
        replyPlId: '', // 回复评论的Id
        replyVal: '' , // 回复的内容
    },
    onLoad:function(op){
     console.log(op.id);
     this.setData({
        id:op.id,
        type:op.type
     })
      getApp().getUserInfo().then(res=>{
      var header={
             'openid':res.openid, //公用请求头
             'content-type': 'application/json'
         }
       this.setData({
        userInfo:res,
        header:header
       })  
    
       this.readBbs();
       this.commentList()
     })

    
    },
    readBbs:function () {
         var _self=this;
         wx.request({
            url: getApp().globalData.baseUrl + '/wx/articles/browse/save',
            method:"POST",
            data:{
              openid:_self.data.userInfo.openid,
              articlesId:_self.data.id,
              type:2
            },
            header: this.data.header,
            success: function(res) {
              _self.getBbsDetail();
            
         }
       })
    },

    getBbsDetail:function(){
       var _self=this;
         wx.request({
            url: getApp().globalData.baseUrl + '/wx/articles/detail?id='+this.data.id+'&openid='+this.data.userInfo.openid,
            method:"GET",
            header: this.data.header,
            success: function(res) {
             console.log(res.data.obj)
             if (res.data.obj.followNum == 1) {
              _self.setData({
                atttionShow:true,
                obj:res.data.obj
               })
            var  article=res.data.obj.content;
           
            WxParse.wxParse('article', 'html', article, _self, 5);
             } else {
              _self.setData({
                atttionShow:false,
                obj:res.data.obj
               })
             } 
            var  article=res.data.obj.content;
           
            WxParse.wxParse('article', 'html', article, _self, 5);
         }
       })
    },
    attionTap () { 
      var _self = this
      if (this.data.atttionShow) {// 取消关注
        wx.request({
          url: getApp().globalData.baseUrl + '/wx/articles/follow/user/delete?openid='+this.data.userInfo.openid+"&articlesId="+this.data.id,
          method:"DELETE",
          data:{
            openid:this.data.userInfo.openid,
            articlesId:this.data.id
          },
          header: this.data.header,
          success: function(res) {
            console.log(res.data.code)
            if (res.data.code==200) {
              _self.getBbsDetail();
            }         
          }
        })
      } else { // 关注
        wx.request({
          url: getApp().globalData.baseUrl + '/wx/articles/follow/user/save',
          method:"POST",
          data:{
            openid:this.data.userInfo.openid,
            articlesId:this.data.id
          },
          header: this.data.header,
          success: function(res) {
            console.log(res.data.code)
            if (res.data.code==200) {
              _self.getBbsDetail();
            }         
          }
        })
      }
    },
    commentList () { // 评论列表
      var _self = this
      wx.request({
        url: getApp().globalData.baseUrl + '/wx/articles/getCommentList?articlesId=' + this.data.id,
        method:"GET",
        header: this.data.header,
        success: function(res) {
         
         if (res.data.code==200) {
          console.log(res.data)
          _self.setData({
            commentItem:res.data.obj
          })
         }         
     }
   })
    },
    turnDetail:function(){
    	wx.navigateTo({
            url: '/pages/communityDetail/communityDetail'
        })
    },
    turnPage:function(){
        if (this.data.type) {

           wx.switchTab({
                  url: '../../pages/home/home'
           })
           
        }else{
            var userInfo= wx.getStorageSync('userInfo');
          if (userInfo.phone==null||userInfo.phone.length!=11) {
             this.setData({
             showToast:true
            })
          }else{
              wx.navigateTo({
              url: '/pages/sendSheQu/sendSheQu'
              })
          }

           
        }
        
    },
     //点赞
    dianzanM:function(e){
         var _self=this;
        var id=e.target.dataset.code;
         wx.request({
            url: getApp().globalData.baseUrl + '/wx/articles/browse/save',
            method:"POST",
            data:{
              openid:this.data.userInfo.openid,
              articlesId:id,
              type:1
            },
            header: this.data.header,
            success: function(res) {
             console.log(res.data.code)
             if (res.data.code==200) {
              _self.getBbsDetail();

             }         
           
         }
       })

    },
    //取消点赞
    dianzanF:function(e){
         var _self=this;
        var id=e.target.dataset.code;
          console.log(e)
         wx.request({
            url: getApp().globalData.baseUrl + '/wx/articles/browse/delete?openid='+this.data.userInfo.openid+'&articlesId='+id,
            method:"DELETE",
            body:{
              openid:this.data.userInfo.openid,
              articlesId:id,
            },
            header: this.data.header,
            success: function(res) {
             console.log(res.data.code)
             if (res.data.code==200) {
            _self.getBbsDetail();

             }         
         }
       })
    },
    bindTextAreaBlur (e) { // 发表评论输入框的值
      this.setData({
        textareaVal:e.detail.value
      })
    },
    publishTap () { // 发表
       if (this.data.textareaVal == '') {
        wx.showToast({
          title: '请输入评论内容',
          icon: 'none',
          duration: 2000
        })
        return
      }
      var _self = this
      wx.request({
        url: getApp().globalData.baseUrl + '/wx/articles/comment/save',
        method:"POST",
        data:{
          openid:this.data.userInfo.openid,
          articlesId:this.data.id,
          content:this.data.textareaVal
        },
        header: this.data.header,
        success: function(res) {
          console.log(res.data.code)
          if (res.data.code==200) {
            _self.commentList();
            _self.setData({
                 textareaVal:''
            })
          }         
        }
      })
    },
    replyTap (e) { // 回复
      if (this.data.replynum == e.target.dataset.index) {
        this.setData({
          replynum:-1,
        })
      } else {
        this.setData({
          replynum:e.target.dataset.index,
          replyPlId:e.target.dataset.id
        })
      }
    },
    replydeteleTap (e) {// 删除评论
      this.deleteReplyComment(e.target.dataset.id,1)
    },
    delcommentTap (e) { // 删除回复
      this.deleteReplyComment(e.target.dataset.id,2)
    },
    deleteReplyComment(id,type) {
      var _self = this
      wx.request({
        url: getApp().globalData.baseUrl + '/wx/articles/comment/reply/delete?openid='+this.data.userInfo.openid+"&id="+id+"&type="+type,
        method:"DELETE",
        header: this.data.header,
        data:{
          openid:this.data.userInfo.openid,
          id:id,
          type:type
        },
        header: this.data.header,
        success: function(res) {
          console.log(res.data.code)
          if (res.data.code==200) {
            _self.commentList();
          }         
        }
      })
    },
    replyInput (e) {
      this.setData({
        replyVal:e.detail.value
      })
    },
    replyBtnTap () { // 回复提交按钮
      console.log(this.data.replyPlId)
      if (this.data.replyVal == '') {
        wx.showToast({
          title: '请输入回复内容',
          icon: 'none',
          duration: 2000
        })
      } else {
        var _self = this
        wx.request({
          url: getApp().globalData.baseUrl + '/wx/articles/comment/reply/save',
          method:"POST",
          data:{
            openid:this.data.userInfo.openid,
            articlesId:this.data.id,
            content:this.data.replyVal,
            articlesCommentId:this.data.replyPlId
          },
          header: this.data.header,
          success: function(res) {
            console.log(res.data.code)
            if (res.data.code==200) {
              _self.commentList();
              _self.setData({
                replynum:-1
              })
            }         
          }
        })
      }
    },
    onShareAppMessage:function(){

        let pages = getCurrentPages();
        let currPage = null;
        if (pages.length) {
           currPage = pages[pages.length - 1];
        }
        console.log(currPage)   
         return {
              title: this.data.obj.title,
              path: '/pages/communityDetail/communityDetail?id='+this.data.id+'&type=share', // 这里的 path 是页面 url，而不是小程序路由
            }
    },
    onShareTimeline:function(){

        let pages = getCurrentPages();
        let currPage = null;
        if (pages.length) {
           currPage = pages[pages.length - 1];
        }
        console.log(currPage)   
         return {
              title: this.data.obj.title,
              query:'id='+this.data.id+'&type=share',
              path: '/pages/communityDetail/communityDetail?id='+this.data.id+'&type=share', // 这里的 path 是页面 url，而不是小程序路由
            }
    },
    onCannel:function () {
     this.setData({
       showToast:false
     })
    },
    onLoginSuccess:function () {
       this.setData({
       showToast:false
      })
    }
   
});